package com.yksj.healthtalk.wallet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.ui.R;

/**
 * 我的钱包
 * @author Administrator
 *
 */
public class MyWalletMainActivity extends BaseFragmentActivity implements OnClickListener{
	
	private MyBalanceFragment mMyBalanceFragment;
	private MyBillFragment mMyBillFragment;
	private PopupWindow mPopupWindow;
	private Boolean isPayPwd;//是否设置支付密码
	private Boolean isBDPhone;//是否绑定手机
	private String isBDPhoneNum;//绑定手机号码
	private RadioGroup mRadioGroup;
	private RadioButton mBillRb;//流水
	private boolean isNoticeSms; 

	 @SuppressLint("HandlerLeak")
	Handler handler=new android.os.Handler(){
		public void handleMessage(android.os.Message msg) {
			showPopWindow();
		}
	};
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.my_wallet_layout);
		initWidget();
		initDate();
	}

	private void initDate() {
		mMyBalanceFragment = new MyBalanceFragment();
		changeFragment(mMyBalanceFragment);
	}

	private void initWidget() {
		initTitle();
		titleTextV.setText("我的钱包");
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("设置");
		mBillRb = ((RadioButton) findViewById(R.id.bill));
		mBillRb.setOnClickListener(this);
		((RadioButton)findViewById(R.id.balance)).setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.bill:
			if (mMyBillFragment == null) {
				mMyBillFragment = new MyBillFragment();
			}
			changeFragment(mMyBillFragment);
			handler.sendEmptyMessage(0);
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.balance:
			changeFragment(mMyBalanceFragment);
			break;
		case R.id.title_right2:
			if (isPayPwd != null && isBDPhone != null) {
				Intent intent = new Intent(getApplicationContext(), MyWalletSettingActivity.class);
				intent.putExtra("isPayPwd", isPayPwd);
				intent.putExtra("isBDPhone", isBDPhone);
				intent.putExtra("isBDPhoneNum", isBDPhoneNum);
				intent.putExtra("isNoticeSms", isNoticeSms);
				startActivityForResult(intent, 1);
			}
			break;

		}
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
	
	/**
	 * 更换Fragment
	 * @param fragment
	 */
	public void changeFragment(Fragment fragment){
		Fragment ft= getSupportFragmentManager().findFragmentById(R.id.fragment);
		if(ft==fragment) return ;
		
		getSupportFragmentManager().popBackStackImmediate();
		FragmentTransaction transaction =  getSupportFragmentManager().beginTransaction();
		transaction.add(R.id.fragment, fragment);
		transaction.addToBackStack(null);
		transaction.commitAllowingStateLoss();
	}
	
	public void showPopWindow(){
		if (mPopupWindow == null) {
			View v = LayoutInflater.from(this).inflate(R.layout.bill_pop, null);
			mRadioGroup = (RadioGroup)v.findViewById(R.id.rp);
			mPopupWindow = new PopupWindow(v, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
			mPopupWindow.setTouchable(true);
			mPopupWindow.setFocusable(true);
			mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					// TODO Auto-generated method stub
					switch (checkedId) {
					case R.id.in:
						mMyBillFragment = new MyBillFragment("IN");
						mBillRb.setText("进账流水");
						changeFragment(mMyBillFragment);
						mPopupWindow.dismiss();
						break;
					case R.id.out:
						mMyBillFragment = new MyBillFragment("OUT");
						changeFragment(mMyBillFragment);
						mBillRb.setText("出账流水");
						mPopupWindow.dismiss();
						break;
					case R.id.in_out:
						mMyBillFragment = new MyBillFragment("");
						mBillRb.setText("进出流水");
						changeFragment(mMyBillFragment);
						mPopupWindow.dismiss();
						break;
					}
				}
			});
		}
		mPopupWindow.showAsDropDown(findViewById(R.id.rp));
	}
	
	/**
	 * 初始化要传递的数据
	 * @param isPayPwd
	 * @param isBDPhone
	 * @param isBDPhoneNum
	 */
	public void  setInitDate(boolean isPayPwd,boolean isBDPhone,String isBDPhoneNum,boolean isNoticeSms){
		this.isPayPwd = isPayPwd;
		this.isBDPhone = isBDPhone;
		this.isBDPhoneNum = isBDPhoneNum;
		this.isNoticeSms = isNoticeSms;
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if (arg0 == 1 && arg1 == RESULT_OK) {
			if (arg2.hasExtra("isNoticeSms")) {
				isNoticeSms = arg2.getBooleanExtra("isNoticeSms", false);
			}
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
	
	
}
